use super::common::*;

/// 获取指定日期距离下一个正月初一要几天.
/// 注: 1970-02-06为正月初一.
pub fn cal_days_to_festival(y: u32, m: u32, d: u32) -> u32 {
    // 当前日期离1970-02-06的日数
    let passed_days = days_since_origin(y, m, d) - days_since_origin(1970, 2, 6);

    // 求第y年春节离1970-02-06的日数
    let mut festival_passed_days: u32 = (1970..y).map(get_days_in_lunar_year).sum();
    // 要等y+1年的春节
    // 测例原因, 除夕日才得到0, 而春节当天则要返回一整年少一天
    if festival_passed_days - 1 < passed_days {
        festival_passed_days += get_days_in_lunar_year(y);
    }

    festival_passed_days - passed_days - 1
}

/// 获取指定农历年的天数.
fn get_days_in_lunar_year(y: u32) -> u32 {
    let mut sum = 348;
    let mut mask = 0x8000;
    while mask > 0x8 {
        if get_lunar_info(y) & mask > 0 {
            sum += 1;
        }
        mask >>= 1;
    }

    sum + leap_days_in_lunar_year(y)
}

/// 查询指定农历年中闰月有几天.
/// 可能为0, 29, 30
fn leap_days_in_lunar_year(y: u32) -> u32 {
    // 判断有无闰月
    if leap_month_numth(y) > 0 {
        // 闰月天数
        if get_lunar_info(y) & 0x10000 > 0 {
            30
        } else {
            29
        }
    } else {
        0
    }
}

/// 查询指定农历年中闰月是哪一月. 0表示无闰月.
fn leap_month_numth(y: u32) -> u32 {
    get_lunar_info(y) & 0xf
}

/// 查询指定年的润大小信息.
fn get_lunar_info(y: u32) -> u32 {
    LUNAR_INFO[y as usize - 1900]
}

/// 农历1900-2100的润大小信息表.
/// 数据来源于香港天文台.
const LUNAR_INFO: [u32; 201] = [
    0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0,
    0x055d2, //1900-1909
    0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0,
    0x14977, //1910-1919
    0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2,
    0x04970, //1920-1929
    0x06566, 0x0d4a0, 0x0ea50, 0x16a95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7,
    0x0c950, //1930-1939
    0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950,
    0x0b557, //1940-1949
    0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950,
    0x06aa0, //1950-1959
    0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57,
    0x056a0, //1960-1969
    0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0,
    0x195a6, //1970-1979
    0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60,
    0x09570, //1980-1989
    0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x05ac0, 0x0ab60, 0x096d5,
    0x092e0, //1990-1999
    0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0,
    0x0cab5, //2000-2009
    0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0,
    0x0a930, //2010-2019
    0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65,
    0x0d530, //2020-2029
    0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520,
    0x0dd45, //2030-2039
    0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20,
    0x0ada0, //2040-2049
    0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06aa0, 0x1a6c4,
    0x0aae0, //2050-2059
    0x092e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0,
    0x055d4, //2060-2069
    0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0,
    0x052b0, //2070-2079
    0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4,
    0x0d160, //2080-2089
    0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150,
    0x0f252, //2090-2099
    0x0d520, //2100
];

// mod tests {
//     #[allow(unused_imports)]
//     use super::*;

//     #[test]
//     fn test_days_in_lunar_year() {
//         // 1970-02-06和1971-01-27为正月初一
//         let lunar_days = get_days_in_lunar_year(1970);
//         let days = days_since_origin(1971, 1, 27) - days_since_origin(1970, 2, 6);
//         assert_eq!(lunar_days, days);
//     }

//     #[test]
//     fn test_cal_days_to_festival() {
//         assert_eq!(cal_days_to_festival(1971, 1, 26), 0);
//         assert_eq!(
//             cal_days_to_festival(1971, 1, 27),
//             get_days_in_lunar_year(1971) - 1
//         );
//         assert_eq!(cal_days_to_festival(2025, 1, 28), 0);
//         assert_eq!(
//             cal_days_to_festival(2025, 1, 29),
//             get_days_in_lunar_year(2025) - 1
//         );
//     }
// }
